package com.itheima.Controller;

import com.itheima.domain.Book;

import java.util.List;
import java.util.Scanner;

public class FindBookAuthor {

    //通过作者来查看书籍
    public String findAuthor(List<Book> list) {
        //获取要查询的作者
        while (true) {
            System.out.println("请输入您要查询的作者(输入exit返回上一级)");
            Scanner sc = new Scanner(System.in);
            String author = sc.next();
            if("exit".equals(author)){
                return null;
            }
            //判断作者是否存在
            boolean exists = isExists(list, author);
            //书籍存在和不存在的操作
            if (!exists) {

                System.out.println("您查询的作者不存在");
            }else {
                return author;
            }
        }
    }

    //判断作者是否存在
    public boolean isExists(List<Book> list, String author) {

        boolean flag = false;

        for (Book book : list) {

            String bookAuthor = book.getBookAuthor();

            if (book != null && author.equals(bookAuthor)) {
                flag = true;
            }
        }
        return flag;
    }

}



